package com.example.allplan.ShuJuKuCaoZuo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.example.allplan.JavaBean.BiaoQingLieBiao;
import com.example.allplan.JavaBean.JiHuaBaoJavaBean;
import com.example.allplan.JavaBean.JiHuaJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.RecyclerAdapter.JiHuaBaoRecyclerAdapter;
import com.example.allplan.SQhelper.SuoYouJiHuaBaoSQhelper;
import com.example.allplan.SQhelper.SuoYouJiHuaSQhelper;

import java.util.ArrayList;

public class SuoYouJiHuaSQCaoZuo {
    public final String JI_HUA_SHU_JU_KU_WEN_JIAN_MING = "ji_hua_shu_ju_ku_wen_jian";
    public SuoYouJiHuaSQhelper mdbhelp;
    public SQLiteDatabase mDatabase;
    public Context mContext;

    public static ContentValues bean_to_values(JiHuaJavaBean o) {
        ContentValues values = new ContentValues();
        values.put("id", o.getId());
        values.put("pai_xu_you_biao", o.getPai_xu_you_biao());
        values.put("lei_xing", o.getLei_xing());
        values.put("suo_shu_ji_hua_bao_id", o.getSuo_shu_ji_hua_bao_id());
        values.put("yuan_suo_shu_id", o.getYuan_suo_shu_id());
        values.put("ming_cheng", o.getMing_cheng());
        values.put("wen_jian_ming", o.getWen_jian_ming());
        return values;
    }
    public static ContentValues bean_to_values_only_three(JiHuaJavaBean o) {
        ContentValues values = new ContentValues();
        values.put("id", o.getId());
        values.put("lei_xing", o.getLei_xing());
        values.put("ming_cheng", o.getMing_cheng());
        return values;
    }
    /* public static ContentValues bean_to_value_only_biao_qing_and_bao_ming(JiHuaBaoJavaBean o){
         ContentValues values=new ContentValues();
         values.put("id",o.getId());
         values.put("biao_qing_fu_hao_zhi",o.getBiao_qing_fu_hao_zhi());
         values.put("bao_ming",o.getBao_ming());
         return values;
     }*/
    public SuoYouJiHuaSQCaoZuo(Context mC) {
        mContext = mC;
        mdbhelp = new SuoYouJiHuaSQhelper(mContext, JI_HUA_SHU_JU_KU_WEN_JIAN_MING, null, 1);
    }

    public synchronized void openRead() {
        mDatabase = mdbhelp.getReadableDatabase();
    }

    public synchronized void close() {
        mDatabase.close();
    }

    public synchronized void openWrite() {
        mDatabase = mdbhelp.getWritableDatabase();
    }

    public synchronized ArrayList<JiHuaJavaBean> reloadAllJiHua() {
        ArrayList<JiHuaJavaBean> mList = new ArrayList<JiHuaJavaBean>();
        openWrite();
        Cursor cursor = mDatabase.query("ji_hua_biao", new String[]{"id", "lei_xing", "suo_shu_ji_hua_bao_id", "pai_xu_you_biao", "yuan_suo_shu_id", "ming_cheng", "wen_jian_ming"}, null, null, null, null, "id");
        while (cursor.moveToNext()) {
            mList.add(new JiHuaJavaBean(cursor.getInt(cursor.getColumnIndex("id")), cursor.getInt(cursor.getColumnIndex("lei_xing")), cursor.getInt(cursor.getColumnIndex("suo_shu_ji_hua_bao_id")), cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")), cursor.getInt(cursor.getColumnIndex("yuan_suo_shu_id")), cursor.getString(cursor.getColumnIndex("ming_cheng")), cursor.getString(cursor.getColumnIndex("wen_jian_ming"))));
        }
        close();
        return mList;
    }

    public synchronized JiHuaJavaBean reloadByIdJiHua(int n) {
        JiHuaJavaBean mo ;
        openWrite();
        Cursor cursor = mDatabase.query("ji_hua_biao", new String[]{"id", "lei_xing", "suo_shu_ji_hua_bao_id", "pai_xu_you_biao", "yuan_suo_shu_id", "ming_cheng", "wen_jian_ming"}, "id=?", new String[]{""+n}, null, null, "id");
        cursor.moveToNext();
        mo = new JiHuaJavaBean(cursor.getInt(cursor.getColumnIndex("id")), cursor.getInt(cursor.getColumnIndex("lei_xing")), cursor.getInt(cursor.getColumnIndex("suo_shu_ji_hua_bao_id")), cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")), cursor.getInt(cursor.getColumnIndex("yuan_suo_shu_id")), cursor.getString(cursor.getColumnIndex("ming_cheng")), cursor.getString(cursor.getColumnIndex("wen_jian_ming")));
        close();
        return mo;
    }

    public synchronized void write_one(JiHuaJavaBean o) {
        openWrite();
        Log.d("1422",o.getMing_cheng());
        mDatabase.insert("ji_hua_biao", null, bean_to_values(o));
        close();
    }

    public synchronized void update_one(JiHuaJavaBean o) {
        openWrite();
        mDatabase.update("ji_hua_biao", bean_to_values(o), "id=?", new String[]{o.getId() + ""});
        close();
    }
    public synchronized void update_one_only_three(JiHuaJavaBean o) {
        openWrite();
        mDatabase.update("ji_hua_biao", bean_to_values_only_three(o), "id=?", new String[]{o.getId() + ""});
        close();
    }
    /*public synchronized void update_one_only_biao_qing_and_bao_ming(JiHuaBaoJavaBean o) {
        openWrite();
        mDatabase.update("ji_hua_bao_biao", bean_to_value_only_biao_qing_and_bao_ming(o), "id=?", new String[]{o.getId() + ""});
        close();
    }*/

    public synchronized void delete_suo_you_chuang_jian() {
        openWrite();
        mDatabase.delete("ji_hua_biao", "id>?", new String[]{"1"});
        close();
    }

    public synchronized void deleteone(JiHuaJavaBean o) {
        openWrite();
        mDatabase.delete("ji_hua_biao","id=?",new String[]{o.getId()+""});
        close();
    }
}
